C++- CPP教程

C++ 标准库 <cwchar>

<cwchar> 是 C++ 标准库中的一个头文件,提供了处理宽字符( wchar_t )和宽字符串的函数。这些函数大部分来自 C 标准库的 <wchar.h> ,用于处理宽字符的输入输出、内存操作、字符串操作和其他与宽字符相关的功能。

语法

cwchar 头文件中定义的函数通常具有与标准字符处理函数相似的名称,但以 w 开头,例如 wprintf wscanf 等。这些函数的参数和返回类型也与相应的标准字符函数不同,它们使用宽字符类型 wchar_t

基本类型

  • wchar_t :宽字符类型,用于存储宽字符。
  • wint_t :用于存储宽字符函数的返回值。

常用函数

1. 宽字符输入输出

  • fgetwc :从文件流中读取宽字符。
  • fputwc :向文件流中写入宽字符。
  • fgetws :从文件流中读取宽字符串。
  • fputws :向文件流中写入宽字符串。
示例代码
#include <cwchar>#include <iostream>intmain(){// 使用 fputws 和 fgetws 进行宽字符串输入输出constwchar_t*filename=L"example.txt";FILE*file=std::fopen("example.txt","w");if(file){std::fputws(L"Hello, 世界!\n", file);std::fclose(file);}file=std::fopen("example.txt","r");if(file){wchar_tbuffer[256];if(std::fgetws(buffer,256, file)){std::wcout<<L"Read from file: "<<buffer;}std::fclose(file);}return0;}

输出结果为:

Read from file: Hello, 世界!

2. 宽字符和宽字符串操作

  • wcscpy :拷贝宽字符串。
  • wcslen :获取宽字符串长度。
  • wcscmp :比较宽字符串。
  • wcsncpy :拷贝指定长度的宽字符串。
示例代码
#include <cwchar>#include <iostream>intmain(){wchar_tstr1[100]=L"Hello";wchar_tstr2[100]=L"World";// 宽字符串拷贝std::wcscpy(str1,L"Hello, 世界!");std::wcout<<L"Copied string: "<<str1<<std::endl;// 宽字符串长度size_tlen=std::wcslen(str1);std::wcout<<L"Length of string: "<<len<<std::endl;// 宽字符串比较intresult=std::wcscmp(str1, str2);std::wcout<<L"Comparison result: "<<result<<std::endl;// 宽字符串部分拷贝std::wcsncpy(str2, str1,5);str2[5]=L'\0';// 确保以空字符结束std::wcout<<L"Partially copied string: "<<str2<<std::endl;return0;}

输出结果为:

Copied string: Hello, 

3. 宽字符分类和转换

  • iswalpha :判断宽字符是否为字母。
  • iswdigit :判断宽字符是否为数字。
  • towlower :将宽字符转换为小写。
  • towupper :将宽字符转换为大写。
示例代码
#include <cwchar>#include <iostream>intmain(){wchar_tch=L'A';// 判断宽字符是否为字母if(std::iswalpha(ch)){std::wcout<<ch<<L" is an alphabetic character."<<std::endl;}// 判断宽字符是否为数字ch=L'9';if(std::iswdigit(ch)){std::wcout<<ch<<L" is a digit."<<std::endl;}// 宽字符转换为小写ch=L'G';wchar_tlower_ch=std::towlower(ch);std::wcout<<L"Lowercase of "<<ch<<L" is "<<lower_ch<<std::endl;// 宽字符转换为大写ch=L'a';wchar_tupper_ch=std::towupper(ch);std::wcout<<L"Uppercase of "<<ch<<L" is "<<upper_ch<<std::endl;return0;}

输出结果为:


A is an alphabetic character.

9 is a digit.

Lowercase of G is g

Uppercase of a is A

4. 宽字符和宽字符串的输入输出

  • wprintf :宽字符格式化输出。
  • wscanf :宽字符格式化输入。
  • swprintf :将格式化宽字符写入宽字符串。
  • swscanf :从宽字符串中读取格式化宽字符。
示例代码
#include <cwchar>#include <iostream>intmain(){wchar_tbuffer[100];// 宽字符格式化输出std::wprintf(L"Formatted output: %d %s\n",42,L"Hello");// 宽字符格式化输入std::wprintf(L"Enter a number and a string: ");std::wscanf(L"%d %ls",&buffer);std::wprintf(L"You entered: %ls\n", buffer);// 将格式化宽字符写入宽字符串std::swprintf(buffer,100,L"Formatted %d %s",42,L"output");std::wcout<<L"Buffer: "<<buffer<<std::endl;// 从宽字符串中读取格式化宽字符intnumber;wchar_tword[100];std::swscanf(buffer,L"Formatted %d %s",&number, word);std::wcout<<L"Parsed number: "<<number<<L", word: "<<word<<std::endl;return0;}

输出结果为:


Formatted output: 42 H

Enter a number and a string: ez4code

You entered: 

Buffer: Formatted 42 o

Parsed number: 42, word: o